import axios from 'axios'
import { ElMessage } from 'element-plus'
import { getToken } from './auth'
const service = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 10000
})
service.interceptors.request.use((config: any) => {
  const isToken = (config.headers || {}).isToken === false
  if(getToken() && !isToken){
    config.headers['Authorization'] = 'Bearer ' + getToken()
  }
  return config
})
service.interceptors.response.use((res) => {
  const code = res.data.code
  const msg = res.data.msg
  if(code == 401) {
    return Promise.reject({reason:"无效的会话，请重新登录"})
  }
  else if (code == 500) {
    ElMessage({ message: msg, type: 'error' })
    return Promise.reject(new Error(msg))
  } else if (code !== 200) {
    return Promise.reject(msg)
  } else {
    return res.data
  }
})

export default service
